<?php

namespace App\Listeners;

use App\Events\UserCommand;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use swoole_client;

class SendCommand
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserCommand  $event
     * @return void
     */
    public function handle(UserCommand $event)
    {
        $client = new swoole_client(SWOOLE_SOCK_TCP);
        if (!$client->connect('127.0.0.1', env('WEB_INNER_PORT')))
        {
            logger("SendCommand connect failed. Error: {$client->errCode}\n");
        }

        $result = $client->send("$event->cmd $event->num $event->deviceCode $event->orderNumber");
        $client->close();

    }

}
